﻿#region License
// Copyright © 2004 - 2010 All Right Reserved, Derek Goslin
// 
// 1. Definitions
// The terms "reproduce," "reproduction," "derivative works," and "distribution" have the same meaning here as under U.S. copyright law.
// A "contribution" is the original software, or any additions or changes to the software.
// A "contributor" is any person that distributes its contribution under this license.
// "Licensed patents" are a contributor's patent claims that read directly on its contribution.
// 
// 2. Grant of Rights
// (A) Copyright Grant- Subject to the terms of this license, including the license
//     conditions and limitations in section 3, each contributor grants you a non-exclusive,
//     worldwide, royalty-free copyright license to reproduce its contribution, prepare derivative 
//     works of its contribution, and distribute its contribution or any derivative works that you create.
// (B) Patent Grant- Subject to the terms of this license, including the license conditions and limitations
//     in section 3, each contributor grants you a non-exclusive, worldwide, royalty-free license under its
//     licensed patents to make, have made, use, sell, offer for sale, import, and/or otherwise dispose of
//     its contribution in the software or derivative works of the contribution in the software.
// 
// 3. Conditions and Limitations
// (A) Reciprocal Grants- For any file you distribute that contains code from the software (in source code or binary format), 
//     you must provide recipients the source code to that file along with a copy of this license, which license will govern that file. 
//     You may license other files that are entirely your own work and do not contain code from the software under any terms you choose.
// (B) No Trademark License- This license does not grant you rights to use any contributors' name, logo, or trademarks.
// (C) If you bring a patent claim against any contributor over patents that you claim are infringed by the software,
//     your patent license from such contributor to the software ends automatically.
// (D) If you distribute any portion of the software, you must retain all copyright, patent, trademark,
//     and attribution notices that are present in the software.
// (E) If you distribute any portion of the software in source code form, you may do so only under this license by including a complete copy 
//     of this license with your distribution. If you distribute any portion of the software in compiled or object code form, you may only do 
//     so under a license that complies with this license.
// (F) The software is licensed "as-is." You bear the risk of using it. The contributors give no express warranties, guarantees or conditions.
//    You may have additional consumer rights under your local laws which this license cannot change. To the extent permitted under your
//    local laws, the contributors exclude the implied warranties of merchantability, fitness for a particular purpose and non-infringement.
#endregion

using System;
using System.Collections.Generic;
using DnEcuDiag.Ecus.Enums;
using DnEcuDiag.Ecus.Iso;
using DnEcuDiag.Factories;
using NUnit.Framework;
using DnEcuDiag.Ecus.Generic;

namespace DnEcuDiag.Tests
{
    [TestFixture]
    public class ABS53Tests
    {
        private Kwp71Ecu _ecu;
        private IsoTestDevice device;

        [TestFixtureSetUp]
        public void Init()
        {
            try
            {
                device = new IsoTestDevice
                {
                    IdentificationData = new List<byte[]>
                                                          {
                                                              new byte[] {0x0F, 02, 0xF6, 0x30, 0x32, 0x37, 0x33, 0x30, 0x30, 0x34, 0x36, 0x32, 0x34, 0x20, 0x20},
                                                              new byte[] {0x0B, 0x04, 0xF6, 0x42, 0x42, 0x20, 0x32, 0x38, 0x39, 0x33, 0x37}
                                                          }.AsReadOnly()
                };

                _ecu = EcuFactory.CreateEcu(EcuFactory.EcuList.Find(obj => obj.Name.Equals("ABS - Bosch 5.3"))) as Kwp71Ecu;
                _ecu.DiagnosticInterface = device;


                _ecu.Connect();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }
        }

        [Test]
        public void TestConnect()
        {
            Assert.AreEqual("This Ecu is used to control safety critical functions. You connect to this control unit at your own risk. Click yes to connect. No to cancel connection.",
                _ecu.EcuWarning);
            Assert.AreEqual("55 A7 86 85 97 9E 79", _ecu.IsoCode);
            Assert.AreEqual("0273004624  ", _ecu.HardwareNumber);
            Assert.AreEqual(string.Empty, _ecu.HardwareVersionNumber);
            Assert.AreEqual("ABS - Bosch 5.3", _ecu.Model);
            Assert.AreEqual(string.Empty, _ecu.OmologationNumber);
            Assert.AreEqual(string.Empty, _ecu.ProductionDate);
            Assert.AreEqual(string.Empty, _ecu.ProgrammingDate);
            Assert.AreEqual(string.Empty, _ecu.SerialNumber);
            Assert.AreEqual(string.Empty, _ecu.SoftwareVersionNumber);
            Assert.AreEqual(string.Empty, _ecu.DrawingNumber);
            Assert.AreEqual("BB 28937", _ecu.SoftwareNumber);
            Assert.AreEqual(0x20, _ecu.EcuAddress);
        }

        [Test]
        public void TestFaults()
        {
            device.Faultcodeinfo = new List<byte[]>
                                         {
                                             new byte[]
                                                 {
                                                     0x0D, 0x12, 0xFC, 0x2C, 0x71, 0x00, 100, 0x40, 0x0B, 0x71, 0x00,
                                                     0x00, 0x40
                                                 }
                                             ,
                                             new byte[]
                                                 {
                                                     0x08, 0x14, 0xFC, 0x2D, 0x78, 0x51, 0xAE, 0x40
                                                 }
                                         };


            _ecu.ReadFaultCodes();

            Assert.AreEqual(3, _ecu.ActiveFaultCodes.Count);

            Console.WriteLine(_ecu.ActiveFaultCodes[0].FaultComponent);
            Console.WriteLine(_ecu.ActiveFaultCodes[0].FaultDescription);
            Assert.AreEqual("002C", _ecu.ActiveFaultCodes[0].FaultComponent);

            Assert.AreEqual(
                "Failed Component Code: ASR off due to intereference\r\n" +
                "Error Code Information: ASR/TC deactivated by the driver (only for systems with ASR/TC)\r\n" +
                "Error Code Information: Check ABS enabled\r\n" +
                "Error Code Information: Brake light switch (BLS) active.\r\n" +
                "Error Code Information: Check ASR/TC InActive (only for systems with ASR/TC)\r\n" +
                "Vehicle Speed: 6.25 km/h\r\n" +
                "Error Count: 64\r\n",
                _ecu.ActiveFaultCodes[0].FaultDescription);

            Console.WriteLine(_ecu.ActiveFaultCodes[1].FaultDescription);
            Console.WriteLine(_ecu.ActiveFaultCodes[1].FaultDescription);
            Assert.AreEqual("000B", _ecu.ActiveFaultCodes[1].FaultComponent);

            Assert.AreEqual(
                "Failed Component Code: Speed Sensor ADX (open circuit)\r\n" +
                "Error Code Information: ASR/TC deactivated by the driver (only for systems with ASR/TC)\r\n" +
                "Error Code Information: Check ABS enabled\r\n" +
                "Error Code Information: Brake light switch (BLS) active.\r\n" +
                "Error Code Information: Check ASR/TC InActive (only for systems with ASR/TC)\r\n" +
                "Vehicle Speed: 0 km/h\r\n" +
                "Error Count: 64\r\n",
                _ecu.ActiveFaultCodes[1].FaultDescription);

            Console.WriteLine(_ecu.ActiveFaultCodes[2].FaultDescription);
            Console.WriteLine(_ecu.ActiveFaultCodes[2].FaultDescription);
            Assert.AreEqual("002D", _ecu.ActiveFaultCodes[2].FaultComponent);

            Assert.AreEqual(
                "Failed Component Code: Sensors power supply (Faulty)\r\n" +
                "Error Code Information: ASR/TC deactivated by the driver (only for systems with ASR/TC)\r\n" +
                "Error Code Information: Check ABS enabled\r\n" +
                "Error Code Information: Brake light switch (BLS) active.\r\n" +
                "Error Code Information: Check ASR/TC InActive (only for systems with ASR/TC)\r\n" +
                "Vehicle Speed: 1306.88 km/h\r\n" +
                "Error Count: 64\r\n",
                _ecu.ActiveFaultCodes[2].FaultDescription);
        }

        [Test]
        public void TestFaultsNone()
        {
            device.Faultcodeinfo = new List<byte[]>();

            _ecu.ReadFaultCodes();

            Assert.AreEqual(0, _ecu.ActiveFaultCodes.Count);
        }

        [Test]
        public void TestSensor()
        {
            Assert.AreEqual(9, _ecu.Sensors.Count);

            TestSensorValues();
        }

        [Test]
        public void TestActuators()
        {
            Assert.AreEqual(13, _ecu.Actuators.Count);

            foreach (IsoActuator actuator in _ecu.Actuators)
            {
                Assert.AreEqual(ActuatorActivationType.Manual, actuator.ActivationType);
            }

            _ecu.ActivateActuator(_ecu.Actuators[0]);
        }

        [Test]
        public void TestPerformanceMeasurment()
        {
            Assert.AreEqual(null, _ecu.SpeedSensor);
            Assert.AreEqual(null, _ecu.RpmSensor);
        }

        private void TestSensorValues()
        {
            int i = 0;
            TimeStampedMeasurement tsm;

            var li = _ecu.Sensors[i] as IsoSensor;
            device.SensorData = new List<Byte[]>
                                    {
                                        new byte[] {0x06, 0x0, 0xFB, 0xFF, 0xFF, 0xFF},
                                        new byte[] {0x0B, 0x0, 0xFB, 0xFF, 0x01, 0x30, 0x10, 0x40, 0x10, 0x50, 0x10},
                                        new byte[] {0x05, 0x0, 0xFB, 0xFF, 0xFF, 0x01}
                                    };

            Console.WriteLine(string.Format("Testing Sensor: [{0}] {1}", i, _ecu.Sensors[i].Name));
            tsm = _ecu.ReadSensor(_ecu.Sensors[i]);
            Assert.AreEqual(1f, tsm.Measurement);
            Assert.AreEqual("Brake light switch active", tsm.Status);
            Assert.AreEqual(0, li.Id);
            i++;

            Console.WriteLine(string.Format("Testing Sensor: [{0}] {1}", i, _ecu.Sensors[i].Name));
            tsm = _ecu.ReadSensor(_ecu.Sensors[i]);
            Assert.AreEqual(1f, tsm.Measurement);
            Assert.AreEqual("Pump motor active", tsm.Status);
            Assert.AreEqual(0, li.Id);
            i++;

            Console.WriteLine(string.Format("Testing Sensor: [{0}] {1}", i, _ecu.Sensors[i].Name));
            tsm = _ecu.ReadSensor(_ecu.Sensors[i]);
            Assert.AreEqual(1f, tsm.Measurement);
            Assert.AreEqual("Valve relay On", tsm.Status);
            Assert.AreEqual(0, li.Id);
            i++;

            Console.WriteLine(string.Format("Testing Sensor: [{0}] {1}", i, _ecu.Sensors[i].Name));
            tsm = _ecu.ReadSensor(_ecu.Sensors[i]);
            Assert.AreEqual(31.9375f, tsm.Measurement);
            Assert.AreEqual(0, li.Id);
            i++;

            Console.WriteLine(string.Format("Testing Sensor: [{0}] {1}", i, _ecu.Sensors[i].Name));
            tsm = _ecu.ReadSensor(_ecu.Sensors[i]);
            Assert.AreEqual(259.0f, tsm.Measurement);
            Assert.AreEqual(0, li.Id);
            i++;

            Console.WriteLine(string.Format("Testing Sensor: [{0}] {1}", i, _ecu.Sensors[i].Name));
            tsm = _ecu.ReadSensor(_ecu.Sensors[i]);
            Assert.AreEqual(260.0f, tsm.Measurement);
            Assert.AreEqual(0, li.Id);
            i++;

            Console.WriteLine(string.Format("Testing Sensor: [{0}] {1}", i, _ecu.Sensors[i].Name));
            tsm = _ecu.ReadSensor(_ecu.Sensors[i]);
            Assert.AreEqual(261.0f, tsm.Measurement);
            Assert.AreEqual(0, li.Id);
            i++;

            Console.WriteLine(string.Format("Testing Sensor: [{0}] {1}", i, _ecu.Sensors[i].Name));
            tsm = _ecu.ReadSensor(_ecu.Sensors[i]);
            Assert.AreEqual(1f, tsm.Measurement);
            Assert.AreEqual("On", tsm.Status);
            Assert.AreEqual(0, li.Id);
            i++;

            Console.WriteLine(string.Format("Testing Sensor: [{0}] {1}", i, _ecu.Sensors[i].Name));
            tsm = _ecu.ReadSensor(_ecu.Sensors[i]);
            Assert.AreEqual(511f, tsm.Measurement);
            Assert.AreEqual(0, li.Id);
            i++;

            device.SensorData = new List<Byte[]>
                                    {
                                        new byte[] {0x06, 0x0, 0xFB, 0x00, 0x00, 0x00},
                                        new byte[] {0x0B, 0x0, 0xFB, 0xFF, 0x01, 0x30, 0x10, 0x40, 0x10, 0x50, 0x10},
                                        new byte[] {0x05, 0x0, 0xFB, 0x00, 0xFF, 0x01}
                                    };

            i = 0;

            Console.WriteLine(string.Format("Testing Sensor: [{0}] {1}", i, _ecu.Sensors[i].Name));
            tsm = _ecu.ReadSensor(_ecu.Sensors[i]);
            Assert.AreEqual(0f, tsm.Measurement);
            Assert.AreEqual("Brake light switch InActive", tsm.Status);
            Assert.AreEqual(0, li.Id);
            i++;

            Console.WriteLine(string.Format("Testing Sensor: [{0}] {1}", i, _ecu.Sensors[i].Name));
            tsm = _ecu.ReadSensor(_ecu.Sensors[i]);
            Assert.AreEqual(0f, tsm.Measurement);
            Assert.AreEqual("Pump motor InActive", tsm.Status);
            Assert.AreEqual(0, li.Id);
            i++;

            Console.WriteLine(string.Format("Testing Sensor: [{0}] {1}", i, _ecu.Sensors[i].Name));
            tsm = _ecu.ReadSensor(_ecu.Sensors[i]);
            Assert.AreEqual(0f, tsm.Measurement);
            Assert.AreEqual("Valve relay Off", tsm.Status);
            Assert.AreEqual(0, li.Id);
            i += 5;

            Console.WriteLine(string.Format("Testing Sensor: [{0}] {1}", i, _ecu.Sensors[i].Name));
            tsm = _ecu.ReadSensor(_ecu.Sensors[i]);
            Assert.AreEqual(0f, tsm.Measurement);
            Assert.AreEqual("Off", tsm.Status);
            Assert.AreEqual(0, li.Id);
        }
    }
}
